package org.platform.service.knowledge;

import com.mc.mic.core.log.bus.BusLogPusher;
import com.mc.mic.core.log.frame.LogBo;
import org.platform.constant.LogType;
import org.platform.dao.mapper.FdKbTreemainMapper;
import org.platform.dao.mapper.FdKbTreenodeMapper;
import org.platform.mo.bussmo.log.OperationMqBO;
import org.platform.mo.csmo.cmd10017.TreeSetRequestParam;
import org.platform.mo.csmo.cmd10017.TreeSetResponseParam;
import org.platform.mo.csmo.cmd10018.TreeQueryRequestParam;
import org.platform.mo.csmo.cmd10018.TreeQueryResponseParam;
import org.platform.mo.csmo.cmd10019.TreeAddRequestParam;
import org.platform.mo.csmo.cmd10019.TreeAddResponseParam;
import org.platform.mo.csmo.cmd10020.TreeDelRequestParam;
import org.platform.mo.csmo.cmd10020.TreeDelResponseParam;
import org.platform.mo.csmo.cmd10021.TreeModifyRequestParam;
import org.platform.mo.csmo.cmd10021.TreeModifyResponseParam;
import org.platform.mo.csmo.common.KB_Tree;
import org.platform.mo.csmo.common.KB_TreeNode;
import org.platform.mo.db.FdKbTreemain;
import org.platform.mo.db.FdKbTreenode;
import org.platform.service.IBaseService;
import org.platform.tool.helper.LogHelper;
import org.platform.tool.util.BeansUtils;
import org.platform.tool.util.ComUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author FengJie
 * @date 2018/4/16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TreeServiceImpl implements IBaseService {
    @Autowired
    private FdKbTreemainMapper treeMapper;
    @Autowired
    private FdKbTreenodeMapper treeNodeMapper;
    @Autowired
    private BusLogPusher mqLog;
    @Autowired
    private LogHelper logHelper;

    /**
     * 查询当前所有的故障树集合（10017）
     */
    public TreeSetResponseParam queryTreeSet(TreeSetRequestParam requestParam) {
        TreeSetResponseParam responseParam = new TreeSetResponseParam();
        List<FdKbTreemain> treemainList = treeMapper.selectAll();
        List<KB_Tree> treeList = BeansUtils.copyListProperties(treemainList, KB_Tree.class);
        responseParam.setTreeList(treeList);
        return responseParam;
    }

    /**
     * 查询正在使用的故障树（10018）
     */
    public TreeQueryResponseParam queryTree(TreeQueryRequestParam requestParam) {
        long treeID = requestParam.getTreeID();
        Example treeExample = new Example(FdKbTreemain.class);
        treeExample.createCriteria().andEqualTo("treeid", treeID);
        List<FdKbTreemain> treemainList = treeMapper.selectByExample(treeExample);
        KB_Tree tree = new KB_Tree();
        if (1 == treemainList.size()) {
            BeanUtils.copyProperties(treemainList.get(0), tree);
        }
        TreeQueryResponseParam responseParam = new TreeQueryResponseParam();
        responseParam.setTreeMain(tree);

        Example treeNodeExample = new Example(FdKbTreenode.class);
        treeNodeExample.createCriteria().andEqualTo("treeid", treeID);
        List<FdKbTreenode> nodeList = treeNodeMapper.selectByExample(treeNodeExample);
        List<KB_TreeNode> treeNodeList = BeansUtils.copyListProperties(nodeList, KB_TreeNode.class);
        responseParam.setTreeNodeList(treeNodeList);

        return responseParam;
    }

    /**
     * 增加故障树（10019）
     */
    public TreeAddResponseParam addTree(TreeAddRequestParam requestParam) {
        mqLog.log(new LogBo<>(LogType.OPERATION_LOG, new OperationMqBO("正在新增故障树")));
        try {
            KB_Tree tree = requestParam.getTreeMain();
            FdKbTreemain treeMain = new FdKbTreemain();
            BeanUtils.copyProperties(tree, treeMain);
            treeMapper.insertSelective(treeMain);
            List<KB_TreeNode> nodeList = requestParam.getTreeNodeList();
            List<FdKbTreenode> treenodeList = BeansUtils.copyListProperties(nodeList, FdKbTreenode.class);
            for (FdKbTreenode node : treenodeList) {
                treeNodeMapper.insertSelective(node);
            }
            logHelper.normal("增加故障树【成功】", null, null);
        } catch (RuntimeException r) {
            logHelper.error("增加故障树【失败】", null, null, r);
        }
        return new TreeAddResponseParam();
    }

    /**
     * 删除故障树（10020）
     */
    public TreeDelResponseParam delTree(TreeDelRequestParam requestParam) {
        try {
            int treeID = requestParam.getTreeID();
            Example treeExample = new Example(FdKbTreemain.class);
            treeExample.createCriteria().andEqualTo("treeid", treeID);
            treeMapper.deleteByExample(treeExample);

            Example treeNodeExample = new Example(FdKbTreenode.class);
            treeNodeExample.createCriteria().andEqualTo("treeid", treeID);
            treeNodeMapper.deleteByExample(treeNodeExample);
            logHelper.normal("删除故障树【成功】", null, null);
        } catch (RuntimeException r) {
            logHelper.error("删除故障树【失败】", null, null, r);
        }
        return new TreeDelResponseParam();
    }

    /**
     * 修改故障树（10021）
     * 客服端可能编辑的时候添加节点信息,所以先删除,再添加
     */
    public TreeModifyResponseParam modifyTree(TreeModifyRequestParam requestParam) {
        try {
            TreeDelRequestParam delParam = new TreeDelRequestParam();
            delParam.setTreeID(ComUtils.longToInteger(requestParam.getTreeMain().getTreeid()));
            delTree(delParam);
            TreeAddRequestParam addParam = new TreeAddRequestParam();
            BeanUtils.copyProperties(requestParam, addParam);
            addTree(addParam);
            logHelper.normal("修改故障树【成功】", null, null);
        } catch (RuntimeException r) {
            logHelper.error("修改故障树【失败】", null, null, r);
        }
        return new TreeModifyResponseParam();
    }
}
